{% extends 'base.html' %}
{% load static %}

{% block content %}
    <div class="container">
        <div class="panel panel-default">
            <div class="panel-heading">柱状图</div>
            <div class="panel-body">
                <div id="m1" style="width: 100%;height: 400px;"></div>
            </div>
        </div>

        <div class="col-sm-8">
            <div class="panel panel-default">
                <div class="panel-heading">折线图</div>
                <div class="panel-body">
                    <div id="m2" style="width: 100%;height: 300px;"></div>
                </div>
            </div>
        </div>
        <div class="col-sm-4">
            <div class="panel panel-default">
                <div class="panel-heading">饼图</div>
                <div class="panel-body">
                    <div id="m3" style="width: 100%;height: 300px;"></div>
                </div>
            </div>
        </div>

    </div>
{% endblock %}

{% block js %}
    <script src="{% static 'js/echarts.min.js' %}"></script>
    <script>
        $(function () {
            intenbar();
            intenpie();
            intenline();
        })

        //初始化柱状图
        function intenbar() {
            var chartDom = document.getElementById('m1');
            var myChart = echarts.init(chartDom);
            var option;

            option = {
                title: {
                    text: '员工部门分析表',
                    subtext: 'Fake Data'
                },
                tooltip: {},
                legend: {
                    data: ['销量'],
                    bottom: 0
                },
                xAxis: {
                    data: []  //后台获取
                },
                yAxis: {},
                series: []  //后台获取
            };

            $.ajax({
                url: '/charts/bar/',
                type: 'get',
                dataType: 'JSON',
                success: function (res) {
                    if (res.stuat) {
                        //将后台返回的数据，更新到option中
                        option.xAxis.data = res.data.data_dict;
                        option.series = res.data.data_list;

                        //生成对应图表
                        myChart.setOption(option);
                    }
                }
            })

        };

        //初始化饼图
        function intenpie() {
            var myChart = echarts.init(document.getElementById('m3'));


            var option = {
                title: {
                    text: '员工薪资表',
                    subtext: 'Fake Data',
                    left: 'center'
                },
                tooltip: {
                    trigger: 'item',
                },
                legend: {
                    orient: 'vertical',
                    left: 'left',
                    bottom: 0
                },
                series: [
                    {
                        name: 'Access From',
                        type: 'pie',
                        radius: '50%',
                        data: [],
                        emphasis: {
                            itemStyle: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };

            $.ajax({
                url: '/charts/pie/',
                type: 'get',
                dataType: 'JSON',
                success: function (res) {
                    if (res.stuat) {
                        //将后台返回的数据，更新到option中
                        option.series[0].data = res.data

                        //生成对应图表
                        myChart.setOption(option);
                    }
                }
            })


        };

        //初始化折线图
        function intenline() {
           var myChart = echarts.init(document.getElementById('m2'));

          var  option = {
                xAxis: {
                    type: 'category',
                    data: []
                },
                yAxis: {
                    type: 'value'
                },
                series: [
                    {
                        data: [],
                        type: 'line'
                    }
                ]
            };

          $.ajax({
                url: '/charts/line/',
                type: 'get',
                dataType: 'JSON',
                success: function (res) {
                    if (res.stuat) {
                        //将后台返回的数据，更新到option中
                        option.xAxis.data=res.data.da
                        option.series[0].data=res.data.db

                        //生成对应图表
                        myChart.setOption(option);
                    }
                }
            })

        }
    </script>
{% endblock %}